package com.bestcem.xm.common.core.grpcplatform.dao.impl;

import cn.hutool.core.lang.Validator;
import com.bestcem.xm.common.core.enums.SearchMatchEnum;
import com.bestcem.xm.common.core.grpcplatform.dao.GrpcPlatformDao;
import com.bestcem.xm.common.core.grpcplatform.dao.mapper.GrpcPlatformMapper;
import com.bestcem.xm.common.core.grpcplatform.entity.*;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.common.core.uitls.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * grpc加签验签dao层实现
 *
 * @author jie.yao <jie.yao@idiaoyan.com>
 * @version v1.0
 * @date 2021/10/12 10:51
 */
@Repository
public class GrpcPlatformDaoImpl implements GrpcPlatformDao {

    @Resource
    private GrpcPlatformMapper grpcPlatformMapper;

    @Override
    public GrpcPlatformDO save(GrpcPlatformDO grpcPlatformDO) {
        GrpcPlatformEntity entity = toEntity(grpcPlatformDO);
        String id = new ObjectId().toHexString();
        entity.setGrpcId(id);
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());
        grpcPlatformMapper.insert(entity);
        return findById(id);
    }

    @Override
    public long updateById(String id, UpdateGrpcPlatformParam param) {
        GrpcPlatformEntity entity = new GrpcPlatformEntity();
        entity.setGrpcId(id);
        entity.setCode(param.getCode());
        entity.setDistributionCode(param.getPCode());
        entity.setName(param.getName());
        entity.setEnabled(param.isEnabled());
        if (Validator.isNotNull(param.getSecretKeys())) {
            entity.setSecretKeys(JsonUtil.transferToJson(param.getSecretKeys()));
        }
        if (Validator.isNotNull(param.getAllowIps())) {
            entity.setAllowIps(JsonUtil.transferToJson(param.getAllowIps()));
        }
        entity.setUpdateTime(DateUtil.getCommonDate());
        return grpcPlatformMapper.updateByEntity(entity);
    }

    @Override
    public GrpcPlatformDO findByCode(String code) {
        return toDO(grpcPlatformMapper.getByCode(code));
    }

    @Override
    public GrpcPlatformDO findById(String id) {
        return toDO(grpcPlatformMapper.getById(id));
    }

    @Override
    public List<GrpcPlatformDO> listGrpcPlatforms(ListGrpcPlatformsParam param) {
        ListGrpcPlatformsQuery query = new ListGrpcPlatformsQuery();
        query.setCode(param.getCode());
        if (Validator.isNotNull(param.getPage()) && Validator.isNotNull(param.getSize())) {
            query.setOffset((param.getPage() - 1) * param.getSize());
            query.setLimit(param.getSize());
        }
        List<GrpcPlatformEntity> grpcPlatformEntityList;
        if (param.getAccurate() == SearchMatchEnum.EXACT.getIndex()) {
            // 精确匹配
            grpcPlatformEntityList = grpcPlatformMapper.listByCode(query);
        } else {
            // 模糊匹配
            grpcPlatformEntityList = grpcPlatformMapper.listLikeCode(query);
        }
        return listToDO(grpcPlatformEntityList);
    }

    @Override
    public Long count(ListGrpcPlatformsParam param) {
        if (param.getAccurate() == SearchMatchEnum.EXACT.getIndex()) {
            // 精确匹配
            return grpcPlatformMapper.countByCode(param.getCode());
        } else {
            // 模糊匹配
            return grpcPlatformMapper.countLikeCode(param.getCode());
        }
    }

    @Override
    public void deleteById(String id) {
        grpcPlatformMapper.deleteById(id);
    }

    /**
     * DO -> entity
     *
     * @param grpcPlatformDO DO
     * @return entity
     */
    private GrpcPlatformEntity toEntity(GrpcPlatformDO grpcPlatformDO) {
        GrpcPlatformEntity entity = new GrpcPlatformEntity();
        entity.setCode(grpcPlatformDO.getCode());
        entity.setDistributionCode(grpcPlatformDO.getPCode());
        entity.setName(grpcPlatformDO.getName());
        entity.setEnabled(grpcPlatformDO.isEnabled());
        if (Validator.isNotNull(grpcPlatformDO.getSecretKeys())) {
            entity.setSecretKeys(JsonUtil.transferToJson(grpcPlatformDO.getSecretKeys()));
        }
        if (Validator.isNotNull(grpcPlatformDO.getAllowIps())) {
            entity.setAllowIps(JsonUtil.transferToJson(grpcPlatformDO.getAllowIps()));
        }
        return entity;
    }

    /**
     * entity -> DO
     *
     * @param entity entity
     * @return DO
     */
    private GrpcPlatformDO toDO(GrpcPlatformEntity entity) {
        GrpcPlatformDO grpcPlatformDO = new GrpcPlatformDO();
        grpcPlatformDO.setId(entity.getGrpcId());
        grpcPlatformDO.setCode(entity.getCode());
        grpcPlatformDO.setPCode(entity.getDistributionCode());
        grpcPlatformDO.setName(entity.getName());
        grpcPlatformDO.setEnabled(entity.getEnabled());
        if (StringUtils.isNotBlank(entity.getSecretKeys())) {
            grpcPlatformDO.setSecretKeys(JsonUtil.jsonToTransfer(entity.getSecretKeys(), new TypeReference<String[]>() {
            }));
        }
        if (StringUtils.isNotBlank(entity.getAllowIps())) {
            grpcPlatformDO.setAllowIps(JsonUtil.jsonToTransfer(entity.getAllowIps(), new TypeReference<String[]>() {
            }));
        }
        grpcPlatformDO.setCreatedDate(entity.getCreateTime());
        grpcPlatformDO.setUpdatedDate(entity.getUpdateTime());
        return grpcPlatformDO;
    }

    /**
     * list : entity -> DO
     *
     * @param grpcPlatformEntityList entityList
     * @return DOList
     */
    private List<GrpcPlatformDO> listToDO(List<GrpcPlatformEntity> grpcPlatformEntityList) {
        return grpcPlatformEntityList.stream().map(this::toDO).collect(Collectors.toList());
    }
}
